layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;

    $(".loginBody .seraph").click(function(){
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
            time:5000
        });
    })

    //登录按钮
    $(function () {
        form.on("submit(login)",function(data){
            // $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");

            var username = $(" input[ name='username' ] ").val().trim();
            var password = $(" input[ name='password' ] ").val().trim();
            var code = $(" input[ name='code' ] ").val().trim();

            if(username.length == 0 || password.length == 0 || code.length == 0 || isNaN(username)) {
                layer.msg("登陆信息有误，无法登陆",{
                    time:3000
                });
            }else {
                // $("#loginform").submit();
                var targetUrl = $("#loginform").attr("action");
                var data = $("#loginform").serialize();

                $.ajax({
                    type:'post',
                    url:targetUrl,
                    cache: false,
                    data:data,  //重点必须为一个变量如：data
                    dataType:'json',
                    success:function(data){
                        // alert("zzz");
                        if(data == 0) {
                            layer.msg("验证码不正确",{
                                time:3000
                            });
                        }
                        if(data == 1) {
                            // alert("用户名或者密码不正确");
                            layer.msg("用户名或者密码不正确",{
                                time:3000
                            });
                        }

                        if(data == 2) {
                            layer.msg("账号已过期，请联系管理员",{
                                time:3000
                            });
                        }

                        if(data == 3) {
                            window.location.href="/";
                        }

                    },
                    error:function(){
                        layer.msg("请求失败，请重试",{
                            time:3000
                        });
                    }
                })

            }
            /*setTimeout(function(){
                window.location.href = "/layuicms2.0";
            },1000);*/
            return false;
        })

    });

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
